package com.eascs.web.o2o.web.bi.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.web.o2o.web.bi.dao.BIDao;
import com.eascs.web.o2o.web.bi.entity.BIReceivableDetail;
import com.eascs.web.o2o.web.bi.vo.LoanDetailForm;

/**
 * 回款信息实现类
 * 
 * @author ycz
 *
 */
@Service
public class ReceivableService {

	@Autowired
	private BIDao biDao;

	/**
	 * 回款明细
	 * 
	 * @param LoanDetailForm
	 * @return
	 */
	public List<BIReceivableDetail> findReceivableDetailByPage(LoanDetailForm loanDetailForm) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT	oeld.id AS physicalId,	oeld.bill_code_ea AS billCode,	oll.loan_id AS customerId,	CONCAT(	LEFT (oeld.loaner_name, 1),	'**') AS customerName,	opr.province_name AS provincial, "
				+ "	CONCAT(	oll.ltd_code,'-',oll.ltd_name) AS ltd,obr.pay_amount AS receivableAmount,	obr.pay_time AS receivableDate FROM	o2o_ea_loan_data obld  LEFT JOIN o2o_badloan_record obr ON obr.loan_data_id=obld.id "
				+ "  LEFT JOIN o2o_ea_loan_data oeld ON oeld.id = obr.loan_data_id LEFT JOIN o2o_loanapply oll ON oll.bill_code = oeld.bill_code_ea LEFT JOIN o2o_platform_regional opr ON oll.ltd = opr.platform_id "
				+ " LEFT JOIN o2o_ea_loan_settle oels ON oeld.id = oels.loan_data_id WHERE	1 = 1 AND obld.pay_type = 'DF' AND obr.pay_amount IS NOT NULL AND obr.flow_status='CLO'"
				+ " AND obr.pay_time IS NOT NULL ");
		if (StringUtil.hasText(loanDetailForm.getStartDate())) {
			sql.append(" and obr.this_payment_date >= ");
			sql.append("'");
			sql.append(loanDetailForm.getStartDate());
			sql.append("'");
		}
		if (StringUtil.hasText(loanDetailForm.getEndDate())) {
			sql.append(" and obr.this_payment_date <  ");
			sql.append("'");
			sql.append(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(loanDetailForm.getEndDate()), 1)));
			sql.append("'");
		}

		sql.append(" order by obr.this_payment_date desc ");
		return biDao.findEntitys(sql.toString(),BIReceivableDetail.class);
	}
}
